
$('#Commonheader').load('/pc/Commonheader.html');
$('#Commonfooter').load('/pc/CommonFooter.html');

$(function(){

	
	var tipID = window.sessionStorage['data-id'];
	
	if( tipID == 0 ){
		$('#main>p').html('<p>您所在的位置：首页&gt;产品详情</p>')
	}else{
		$('#main>p').html('<p>您所在的位置：产品服务&gt;产品详情</p>')
	}

    $('#product_detail_list').on('click','li',function(){
        "use strict";
        $(this).addClass('active').siblings('.active').removeClass('active');
    })
    
    var productID = window.sessionStorage['dataId'];
    
    $('#hotProduct_list').children(`[data-show=${productID}]`).hide();

    $.ajax({
    	url:'/product/getProduct',
    	type:'get',
    	data:{
    		productID:productID
    	},
    	success:function(data){
    		/*window.sessionStorage['itemType'] = data.product;*/
    		var cycleListHTML = '';
    		for(var i=0;i<data.length;i++){
    			var product = data[i].product,
    				content = data[i].content,
    				cycle = data[i].cycle,
    				fee = data[i].fee,
    				info = data[i].info,
    				rate = data[i].rate,
    				returnType = data[i].returnType,
    				moneyMax = data[i].moneyMax,
    				moneyMin = data[i].moneyMin;
    			//判断信息名字和logo
    			if(product == '明云'){
        			$('#product_logo').attr('src','/pc/img/mingyunzhuling-chanpingxiangqin-tupian-.png');
        		}else if(product == '国逸'){
        			$('#product_logo').attr('src','/pc/img/guoyizuling-chanpingxiangqingye-tupiao.png');
        		}else{
        			$('#product_logo').attr('src','/pc/img/changanjinrong-chanpinxiangqingye-tupian.png');
        		}
    			
    			//判断显示几期
    			if(i==0){
    				cycleListHTML += `
	    				<li class="cycle active" data_cycleID='${i}' data_cycle='${data[0].cycle}'>${data[0].cycle}借款</li>
	    			`
    			}else{
    				cycleListHTML += `
	    				<li class="cycle" data_cycleID='${i}' data_cycle='${data[i].cycle}'>${data[i].cycle}借款</li>
	    			`
    			}
    			$('#cycleList').html(cycleListHTML);
    			
    			//初始化数据
    			$('.product_name').html(data[0].product+'车抵贷')
        		$('#product_qs').html(data[0].cycle);
        		$('#product_je').html(data[0].moneyMin+'-'+data[0].moneyMax+'万');
        		$('#product_sxf').html(data[0].fee);
        		$('#product_lv').html(data[0].rate);
        		$('#product_fs').html(data[0].returnType);

        		//产品所需资料渲染
        		var apply_dataHTML = '';
        		for(var r=0;r<data[0].info.length;r++){
        			apply_dataHTML += `
        				<li class="product_detail_main">${data[0].info[r]}</li>
        			`
        		}
        		$('#apply_data').html(apply_dataHTML);
        		
        		//产品优势渲染
        		var product_advantageHTML = '';
        		for(var j=0;j<data[0].content.length;j++){
        			product_advantageHTML += `
        				<li class="product_advantage_main">
    	                    <p class="product_advantage_main_tip">${data[0].content[j].point}</p>
    	                    <p class="product_advantage_main_section">${data[0].content[j].good}</p>
    	                </li>
    	    		`
        		}
        		$('#product_advantage').html(product_advantageHTML);
        		
        		//当点击期数时  根据期数显示数据的不同
        	    $('#cycleList').on('click','li',function(){
        	        $(this).addClass('active').siblings('.active').removeClass('active');
        	        var data_cycleID = $(this).attr('data_cycleID');
        	        var data_cycle = $(this).attr('data_cycle');
        	        var type = data.returnType;
        	        var rate = data.rate;
        	        $('#product_qs').html(data_cycle);
        	        $('#product_fs').html(data[data_cycleID].returnType);
        	        $('#product_lv').html(data[data_cycleID].rate);
        	        
        	        var product_advantageHTML = '';
        	        for(var j=0;j<data[data_cycleID].content.length;j++){
            			product_advantageHTML += `
            				<li class="product_advantage_main">
        	                    <p class="product_advantage_main_tip">${data[data_cycleID].content[j].point}</p>
        	                    <p class="product_advantage_main_section">${data[data_cycleID].content[j].good}</p>
        	                </li>
        	    		`
            		}
            		$('#product_advantage').html(product_advantageHTML);
        	    })
        		
    		}
    		
    		
    	}
    })

    
    $('.show_detail_btn').on('click','a',function(e){
    	e.stopPropagation();
    	var productID = $(this).attr('data-id');
    	 window.sessionStorage['dataId'] = productID;
    })

  //省市联动
    var prov = window.XXarea.pres,
        city = window.XXarea.citys;
    var provHtml = '',
        cityHtml = '';
    //省
    for( var i=0;i<prov.length;i++ ){
    	provHtml += `
	    	<option value="${i}" data-val='${prov[i]}'>${prov[i]}</option>
    	`
    }
    $('#prov').html(provHtml);


    //当省发生改变时  修改市的值
    $('#prov').change(function(){
        cityHtml = '';
        var provVal = $(this).val();
        for( var c=0;c<city[provVal].length;c++ ){
            cityHtml += `
    	    	<option value="${c}" data-val='${city[provVal][c]}'>${city[provVal][c]}</option>
        	`
        }
        $('#city').html(cityHtml);
    })
    
    var PhoneReg = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
    
    
    $('#pwd').blur(function(){
    	var pwd = $('#pwd').val();
    	if( PhoneReg.test(pwd) == false ){
			$('#pwd').addClass('err');
			$('#pwd').val('');
			$('#pwd').attr('placeholder','您输入的号码有误');
    	}else{
    		$('#pwd').removeClass('err');
    	}
    })
    $('#pwd').focus(function(){
    	$('#pwd').removeClass('err');
		$('#pwd').attr('placeholder','请输入您的电话');
    })
    
    $('#apply_btn').click(function(){
        for( var i=0;i<prov.length;i++ ){
        	if( $('#prov').val() == i){
        		for(var j=0;j<city[i].length;j++){
        			if( $('#city').val() == j ){
        				var userName = $('#userName').val();
        				var pwd = $('#pwd').val();
        				var add = prov[i]+','+city[i][j];
        				var itemType = window.sessionStorage['itemType']
        				
        					$.ajax({
            					url:'/home/fastCreateOrder',
            					type:'get',
            					data:{
            						name:userName,
            						phone:pwd,
            						area:add,
            						itemType:itemType
            					},
            					success:function(data){
            						console.log(data);
            						if(data.code==1){
            							$('#success_tip').show();
            						}
            						
            					}
            				})
        				
        				
        			}
        		}
        	}
        }
    })
    
    $('#success_tip').click(function(e){
    	e.stopPropagation();
    	$(this).hide();
    })
    
});




















